
package it.bd.SetAllConnectionsOff.helper;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;

import java.util.List;

public class GpsHelper extends AServiceHelper {

    public static final String PREFS_KEY = "gps";
    private static GpsHelper   instance  = null;

    public static GpsHelper getInstance() {
        if (instance == null) {
            Log.e("", "GpsHelper not initialized !");
        }
        return instance;

    }

    private ContextWrapper context;

    public GpsHelper(ContextWrapper context) {
        this.context = context;
        instance = this;
    }

    @Override
    ContextWrapper getContext() {
        return context;
    }

    @Override
    String getPrefKey() {
        return PREFS_KEY;
    }

    @Override
    public boolean isServiceEnabled() {
        LocationManager mGpsService = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
        return mGpsService.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    @Override
    public boolean isServicePresent() {
        LocationManager mgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (mgr == null) {
            return false;
        }
        List<String> providers = mgr.getAllProviders();
        if (providers == null) {
            return false;
        }
        return providers.contains(LocationManager.GPS_PROVIDER);
    }

    @Override
    public void setServiceEnabled(boolean serviceEnabled) {
        Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
        intent.putExtra("enabled", serviceEnabled);
        context.sendBroadcast(intent);
    }
}
